package com.aaron.test;

import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.function.Function.identity;

public class T2 {
    public static final String[] SPECIAL_ADDR = {"名人公馆"};

    public static void main(String[] args) {
        String address = "wg";
        String reserved = "奉贤区";

        if (StringUtils.isBlank(address)) {
            System.out.println("");
        }
        address = address.replaceAll("(\\d)", "*");//数字脱敏
        int firstIndex = 0;
        if (!StringUtils.isBlank(reserved)) {
            int i = address.indexOf(reserved);
            if (i != -1) {
                firstIndex = i + reserved.length();
                //防止数组越界异常
                if (firstIndex >= address.length()) {
                    System.out.println(address);
                }
            }
        }
        //对地址标识无法确定的地址进行脱敏
        for (String special : SPECIAL_ADDR) {
            if (address.contains(special)) {
                address.replace(special, Arrays.stream("special".split("")).map(t -> "*").collect(Collectors.joining()));
            }
        }
        Supplier<Stream<String>> supplier = () -> Stream.of("州", "县", "市", "乡", "镇", "村", "居", "街", "路", "号", "弄", "层", "室", "楼","区");
        final int lastIndex = supplier.get()
                .map(address::lastIndexOf)
                .max(Comparator.comparing(identity()))
//				.map(m -> m == -1 ? address.length() : m)
                .orElseThrow(RuntimeException::new);
        if (lastIndex == -1) {
            //未匹配到地址关键字，直接返回
            System.out.println(address);
        }
    }
}
